package com.credit.jumu.calculator.fragment;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.astuetz.PagerSlidingTabStrip;
import com.credit.jumu.calculator.R;
import com.credit.jumu.calculator.base.BaseFragment;
import com.credit.jumu.calculator.utils.DensityUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * Created by Administrator on 2017/11/23/023.
 */
@EFragment(R.layout.fragment_other)
public class OtherFragment extends BaseFragment {
	@ViewById
	PagerSlidingTabStrip tabStrip;
	@ViewById
	ViewPager otherPager;
	String[] title;

	@AfterViews
	public void initView() {
		title = new String[]{"商业贷款", "公积金贷款", "组合贷款"};
		otherPager.setAdapter(new myPagerAdapter(getFragmentManager(), title));
		tabStrip.setViewPager(otherPager);
		otherPager.setOffscreenPageLimit(0);
		setTabsValue();
	}

	private void setTabsValue() {
		tabStrip.setTextColor(new ColorStateList(
				new int[][]{
						new int[]{android.R.attr.state_pressed}, //pressed
						new int[]{android.R.attr.state_selected}, // enabled
						new int[]{} //default
				},
				new int[]{
						Color.rgb(68, 111, 226),
						Color.rgb(68, 111, 226),
						Color.rgb(179, 179, 179)
				}));
		tabStrip.setPaddingIndicator(DensityUtil.dip2px(getContext(), 100));
	}

	class myPagerAdapter extends FragmentPagerAdapter {
		AccumulationFragment_ accumulationFragment;
		BlendFragment_ blendFragment;
		CommercialFragment_ commercialFragment;
		String[] titles;

		public myPagerAdapter(FragmentManager fm, String[] titles) {
			super(fm);
			this.titles = titles;
		}

		@Override
		public Fragment getItem(int position) {
			hideKeyboard(getActivity());
			switch (position) {
				case 0:
					if (null == commercialFragment) {
						commercialFragment = new CommercialFragment_();
					}
					return commercialFragment;
				case 1:
					if (null == accumulationFragment) {
						accumulationFragment = new AccumulationFragment_();
					}
					return accumulationFragment;
				case 2:
					if (null == blendFragment) {
						blendFragment = new BlendFragment_();
					}
					return blendFragment;
				default:
					return null;
			}
		}

		@Override
		public int getCount() {
			return titles.length;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return titles[position];
		}
	}
}
